package cn.itsource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>HelloWorld</p>
 *
 * @author 波波老师(weixin ： javabobo0513)
 */
@RestController
public class HelloWorld {

    //前端传来的数据，保存到t_account表
    @PostMapping(value = "/save")
    public Map save(@RequestBody Map<String, Object> param) {
        Map<String, Object> result = new HashMap<>();
        JDBCUtils.addAccount(param);
        result.put("result", "true");
        result.put("message", "操作成功");
        return result;
    }

    //查询用户所有记账记录
    @GetMapping(value = "/list")
    public Map listPost() {
        Map<String, Object> result = new HashMap<>();
        //1、查询首页头部信息，也就是某年某月的总收入和总开支
        //账目类型（1：收入；2：支出）
        BigDecimal shouruNum = JDBCUtils.getUserAccount(1);//获取总收入

        BigDecimal zhichuNum = JDBCUtils.getUserAccount(2);//获取总支出

        //获取当前用户所有记账记录
        List<Map<String, Object>> userAccountList = JDBCUtils.getUserAccountDetail();
        //按照time字段进行分组
        Map<String, List<Map<String, Object>>> newMap = userAccountList.stream().collect(Collectors.groupingBy(x -> x.get("time").toString()));

        //封装返回值给前端
        result.put("totalShouru", shouruNum);
        result.put("totalZhichu", zhichuNum);
        result.put("userAccountList", newMap);
        return result;
    }

    @GetMapping("/hello")
    public String hello(){
        return "老弟，来啦";
    }
}
